import 'src/assets/js/rem'
import "./index.less"
import $ from 'zepto-webpack'
import {apiConfig, api, version} from "../../config";
import {someReg} from "../../utils/util";
import {getRequestParams} from "../../utils/requestParams";


function setBottomBg() {
  var bootomElement = document.getElementsByClassName('bottom-bg')[0];
  var topBoxHeight = document.getElementsByClassName('top-box')[0].clientHeight;
  bootomElement.style.top = topBoxHeight - 1 + 'px';
}

window.reset = function reset() {
  setTimeout(function () {
    var scrollHeight = document.documentElement.scrollTop || document.body.scrollTop || 0;
    window.scrollTo(0, Math.max(scrollHeight - 1, 0));
  }, 100)
};

window.onload=function(){
  setBottomBg();
};


$(function () {
  apiConfig.channel = getRequestParams('channel');
  run();
  //点击登录或注册；
  $('.registerBtn').on('click', function () {
    var phone = $("input[name='phone']").val();
    var sms = $("input[name='sms']").val();
    if (!apiConfig.channel) {
      toast('渠道不存在');
      return false
    }
    if (!phone) {
      toast('请输入手机号');
      return false
    }
    if (!someReg.phone.test(phone)) {
      toast('请输入正确手机号');
      return false;
    }
    var registerBtn = $(this);
    registerBtn.attr('disabled', 'disabled');
    var Params = {
      phone: phone,
      sms_code: sms,
      channel: apiConfig.channel ? apiConfig.channel : '',
    };
    var RequestParasm = getRequestParams(Params);

    $.ajax({
      type: 'POST',
      url: apiConfig.domain + '/' + version.api_version + api.no_code_login,
      data: RequestParasm,
      success: function (res) {
        registerBtn.removeAttr('disabled');
        var ResultData = JSON.parse(res);
        if (ResultData.code === 200) {
          // toast('登录成功');
          sessionStorage.setItem(apiConfig.mmh_user_info, JSON.stringify(ResultData.data));
          window.location.href = './submit-loan-info.html';
        } else {
          toast(ResultData.msg);
        }
      },
      error: function (error) {
        registerBtn.removeAttr('disabled')
      }
    })

  });

  //点击发送短信按钮。
  $('.code_btn').on('click', function () {
    var phone = $("input[name='phone']").val();
    if (!phone) {
      toast('请输入手机号');
      return false
    }
    if (!phoneReg.test(phone)) {
      toast('请输入正确手机号');
      return false;
    }
    $.ajax({
      type: 'POST',
      url: apiConfig.domain + '/' + version.api_version + api.send_sms,
      data: getRequestParams({phone: phone}),
      success: function (res) {
        var ResultData = JSON.parse(res);
        if (ResultData.code === 200) {
          toast('短信发送成功');
          CountDown();
        } else {
          toast(ResultData.msg);
        }

      },
      error: function (error) {
        toast('短信发送失败');
      }
    })
  });


});

function toast(text) {
  $('.toast-box').addClass('show').html(text);
  setTimeout(function () {
    $('.toast-box').removeClass('show').html('');
  }, 2500)
}

function run() {
  var textNum = document.getElementById('textNum')
  var initNum = 0;
  var num = 500000;
  var set = setInterval(function () {
    if (initNum >= num) {
      textNum.innerHTML = format(num)
      clearInterval(set)
    } else {
      initNum += 15625;
      if (initNum > 1000) {
        textNum.innerHTML = format(initNum)
      } else {
        textNum.innerHTML = initNum;
      }
    }
  }, 50)
}

function format(n) {
  var num = n.toString();
  var decimals = '';
  // 判断是否有小数
  num.indexOf('.') > -1 ? decimals = num.split('.')[1] : decimals;
  var len = num.length
  if (len <= 3) {
    return num
  } else {
    var temp = ''
    var remainder = len % 3
    decimals ? temp = '.' + decimals : temp
    if (remainder > 0) { // 不是3的整数倍
      return num.slice(0, remainder) + ',' + num.slice(remainder, len).match(/\d{3}/g).join(',') + temp
    } else { // 是3的整数倍
      return num.slice(0, len).match(/\d{3}/g).join(',') + temp
    }
  }
}



